package com.collage.util;

import com.alibaba.excel.EasyExcel;

import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.List;

public class ExcelUtil {
    /**
     * excel导出
     * @param response
     * @param name
     * @param sheetName
     * @param cls
     * @param list
     */
    public static void ExportExcel(HttpServletResponse response, String name, String sheetName, Class<?> cls, List<?> list){
        String fileName = name + ".xlsx";
        try {
            OutputStream out = response.getOutputStream();
            response.setContentType("application/octet-stream");
            response.addHeader("filename", URLEncoder.encode(fileName, "UTF-8"));
            response.addHeader("Access-Control-Expose-Headers", "filename");
            EasyExcel.write(out, cls).sheet(sheetName).doWrite(list);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
